---
title: "EntityFramework 代码段"
categories:
- dotnet
- entityframework
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orga973b21">EntityFramework Code Snippets</a>
<ul>
<li>
<ul>
<li><a href="#org0a93b57">Get mapping in Entityframework</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orga973b21">
<h2 id="orga973b21">EntityFramework Code Snippets</h2>
<div class="outline-text-2" id="text-orga973b21">
</div>
<div class="outline-4" id="outline-container-org0a93b57">
<h4 id="org0a93b57">Get mapping in Entityframework</h4>
<div class="outline-text-4" id="text-org0a93b57">
<div class="org-src-container">
<pre class="src src-cs">using (var context = new BloggingContext())
    {
	var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace;
	var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace));

	var entityType = metadata.GetItems&lt;EntityType&gt;(DataSpace.OSpace)
	    .Single(e =&gt; objectItemCollection.GetClrType(e) == typeof(Blog));

	var entitySet = metadata.GetItems&lt;EntityContainer&gt;(DataSpace.CSpace)
	    .Single().EntitySets
	    .Single(s =&gt; s.ElementType.Name == entityType.Name);

	 var mapping = metadata.GetItems&lt;EntityContainerMapping&gt;(DataSpace.CSSpace).Single()
	    .EntitySetMappings
	    .Single(s =&gt; s.EntitySet == entitySet);

	 var table = mapping.EntityTypeMappings.Single()
	    .Fragments.Single()
	    .StoreEntitySet;

	 var tableName = (string)table.MetadataProperties["Table"].Value ?? table.Name;
    }
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
